package com.yedean.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yedean.mall.api.CommonPage;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.form.PmsBrandParam;
import com.yedean.mall.entity.PmsBrand;
import com.yedean.mall.service.IPmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 品牌功能Controller
 *
 * @author yedean
 * @date 2022/8/11
 */

@Api(tags = "商品品牌管理")
@RequestMapping("/admin/brand")
@RestController
public class PmsBrandController {
    @Autowired
    private IPmsBrandService brandService;

    @ApiOperation(value = "获取全部品牌列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public CommonResult<List<PmsBrand>> getList() {
        return CommonResult.success(brandService.list());
    }


    @ApiOperation(value = "添加品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<?> create(@Valid @RequestBody PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        brandService.save(pmsBrand);
        return CommonResult.success();
    }

    @ApiOperation(value = "更新品牌")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult<?> update(@PathVariable("id") Long id,
                                  @Validated @RequestBody PmsBrandParam pmsBrandParam) {

        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setId(id);
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);

        brandService.saveOrUpdate(pmsBrand);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除品牌")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<?> delete(@PathVariable("id") Long id) {
        brandService.removeById(id);
        return CommonResult.failed();
    }


    @ApiOperation(value = "根据品牌名称分页获取品牌列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsBrand>> getList(@RequestParam(value = "keyword", required = false) String keyword,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        IPage<PmsBrand> brands = brandService.listBrand(keyword, pageNum, pageSize);
        return CommonResult.success(CommonPage.resetPage(brands));
    }

    @ApiOperation(value = "根据编号查询品牌信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<PmsBrand> getItem(@PathVariable("id") Long id) {
        return CommonResult.success(brandService.getById(id));
    }

    @ApiOperation(value = "批量删除品牌")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    public CommonResult<?> deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean remove = brandService.removeBatchByIds(ids);
        if (remove) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量更新显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    public CommonResult<?> updateShowStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("showStatus") Integer showStatus) {


        boolean updateBatch = brandService.updateShowStatus(ids, showStatus);
        if (updateBatch) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation(value = "批量更新厂家制造商状态")
    @RequestMapping(value = "/update/factoryStatus", method = RequestMethod.POST)
    public CommonResult<?> updateFactoryStatus(@RequestParam("ids") List<Long> ids,
                                            @RequestParam("factoryStatus") Integer factoryStatus) {
        boolean update = brandService.updateFactoryStatus(ids, factoryStatus);
        if (update) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


}
